package edu.gatech.cs2340.tigershroud;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * Handles all database administration calls for the User database.
 * @author Cameron Hord
 *
 */
public class UsersDataSource {

	// Database fields
	private SQLiteDatabase usersdb;
	private UserSQLiteHelper dbHelper;
	private String[] allColumns = { UserSQLiteHelper.COLUMN_ID,
			UserSQLiteHelper.COLUMN_NAME, UserSQLiteHelper.COLUMN_PASS };

	public UsersDataSource(Context context) {
		dbHelper = new UserSQLiteHelper(context);
	}

	/**
	 * Gets a database that the app can write to.
	 * @throws SQLException if there is no database to open
	 */
	public void open() throws SQLException {
		usersdb = dbHelper.getWritableDatabase();
	}

	/**
	 * Closes the database after use.
	 */
	public void close() {
		dbHelper.close();
	}
	
	/**
	 * Creates a user and puts the necessary info in the database.
	 * @param name The user's username
	 * @param pass The user's password
	 * @return The user object of the newly created user.
	 */
	public User createUser(String name, String pass) {
		ContentValues values = new ContentValues();
		values.put(UserSQLiteHelper.COLUMN_PASS, pass);
		values.put(UserSQLiteHelper.COLUMN_NAME, name);
		
		long insertId = usersdb.insert(UserSQLiteHelper.TABLE_USERS, null,
				values);
		// To show how to query
		Cursor cursor = usersdb.query(UserSQLiteHelper.TABLE_USERS,
				allColumns, UserSQLiteHelper.COLUMN_ID + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		return cursorToUser(cursor);
	}
	
	/**
	 * Queries the db for a user.
	 * @param name The username of the desired user for which we are searching.
	 * @return The user if found. Otherwise, null.
	 */
	public User findUser(String name) {
		Cursor cursor = usersdb.query(UserSQLiteHelper.TABLE_USERS,
				allColumns, UserSQLiteHelper.COLUMN_NAME + " = '" + name + "'", null,
				null, null, null);
		if(cursor.moveToFirst()){
			return cursorToUser(cursor);	
		}
		
		return null;
	}

	/**
	 * Removes a user from the database.
	 * @param user The username of the user to be removed.
	 */
	public void deleteUser(User user) {
		long id = user.getId();
		System.out.println("Comment deleted with id: " + id);
		usersdb.delete(UserSQLiteHelper.TABLE_USERS, UserSQLiteHelper.COLUMN_ID
				+ " = " + id, null);
	}

	/**
	 * Returns a list object of all the users in the db. Used by admins and for
	 * debugging.
	 * @return The list of all users in the db.
	 */
	public List<User> getAllUsers() {
		List<User> users = new ArrayList<User>();
		Cursor cursor = usersdb.query(UserSQLiteHelper.TABLE_USERS,
				allColumns, null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			User comment = cursorToUser(cursor);
			users.add(comment);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return users;
	}

	/**
	 * Takes in a cursor and returns a user object containing all necessary
	 * data from the top line of the cursor.
	 * @param cursor A query result for a user
	 * @return A user object containing all necessary data from the top 
	 * line of the cursor.
	 */
	private User cursorToUser(Cursor cursor) {
		User user = new User();
		user.setId(cursor.getLong(0));
		user.setName(cursor.getString(1));
		user.setPass(cursor.getString(2));
		return user;
	}
	
	/**
	 * This method clears the database
	 */
	public void dbClear(){
		dbHelper.onUpgrade(usersdb, 1, 1);
	}
}
